package com.example.dml.observer_pattern.event;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Authors：gaoyang
 * @CreateTime：2021/11/2 23:08
 * @Description：监听器，它就是观察者的桥梁
 */
public class EventListener {

    //jdk底层的listener通常也是这么设计
    protected Map<String,Event> events=new HashMap<>();

    //通过事件名称和一个目标对象来触发事件
    public void addListener(String eventType,Object target){
        try {
            this.addListener(
                    eventType,
                    target,
                    target.getClass().getMethod("on"+toUpperFirstCase(eventType),Event.class));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void addListener(String eventType, Object target, Method callback){
        //注册事件
        events.put(eventType,new Event(target,callback));
    }

    //触发，只要有动作就触发
    private void trigger(Event event){
        event.setSource(this);
        event.setTime(System.currentTimeMillis());
        try {
            //发起回调
            if (event.getCallBack()!=null){
                //用发射调用它的回调函数
                event.getCallBack().invoke(event.getTarget(),event);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //事件名称触发
    protected void trigger(String trigger){
        if (!this.events.containsKey(trigger)){return;}
        trigger(this.events.get(trigger).setTrigger(trigger));
    }

    //首字母大写私有处理方法
    private String toUpperFirstCase(String string){
        char[] chars = string.toCharArray();
        chars[0]-=32;
        return String.valueOf(chars);
    }
}
